package source.events.action;

import source.model.*;
import source.model.utility.*;
import source.model.type.*;
import source.view.MainScreen;

public class MakeArmy implements Action{

	private Unit[] units;
	private int pid;
	
	public MakeArmy(Unit[] units, int pid){
		this.units = units;
		this.pid = pid;
	}

	public ActionResult execute( DataHandler dh ) {
		boolean armyfucked = false;

		if(units.length == 0){
			MainScreen.writeToConsole("Cannot create an army of no units");
			return ActionResult.FAILED;
		}
	
		Position armyPos = dh.positionQuery(units[0].objectID());

		for(Unit unit : units){
			if(!armyPos.isEquals(dh.positionQuery(unit.objectID())))
				armyfucked = true;
		}

		if(armyfucked){
			MainScreen.writeToConsole("Not all units are on the same tile");
			return ActionResult.FAILED;	
		}

		Army army = new Army();

		army.add(units);
		if(dh.unassign((GameObject)army, units, armyPos, pid)) {
			MainScreen.writeToConsole("Army successfully created");
			return ActionResult.SUCCESS;
		}
		else {
			MainScreen.writeToConsole("Army failed to be created");
			return ActionResult.FAILED;
		}

	}
}			
